#include "XConnection.hpp"

namespace icecode {
  namespace graphics {

    XConnection* XConnection::_glob = 0;

    XConnection::XConnection(std::string d_name) : screen(0)
    {
      if(!d_name.empty())
	dpy = XOpenDisplay(d_name.c_str());
      else
	dpy = XOpenDisplay(0);
    }

    XConnection::~XConnection()
    {
      close();

      if(_glob == this)
	_glob = reinterpret_cast<XConnection*>(0);
    }

    void XConnection::close()
    {
      if(dpy)
	{
	  XCloseDisplay(dpy);
	  dpy = 0;
	}
    }

    void XConnection::makeGlobal() throw()
    {
      _glob = this;
    }
  }
}
